package hibernate.method;

import hibernate.method.interfaces.RateMethodsInterface;
import hibernate.model.GamesRate;
import hibernate.model.LocalRate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Restrictions;

public class RateMethods implements RateMethodsInterface{

	@Override
	public void addGameRate(SessionFactory sessionFactory, Session session,
			int user_id, int game_id, int rate) {
		GamesRate gr = new GamesRate();
		gr.setGame_id(game_id);
		gr.setUser_id(user_id);
		gr.setRate(rate);
		session.save(gr);
		
	}

	@Override
	public void addLocalRate(SessionFactory sessionFactory, Session session,
			int user_id, int local_id, int rate) {
		LocalRate lr = new LocalRate();
		lr.setUser_id(user_id);
		lr.setLocal_id(local_id);
		lr.setRate(rate);
		session.save(lr);
		
	}

	@Override
	public void editGameRate(SessionFactory sessionFactory, Session session,
			GamesRate r, int rate) {
	r.setRate(rate);
		session.save(r);
	}

	@Override
	public void editLocalRate(SessionFactory sessionFactory, Session session,
			LocalRate r, int rate) {
		r.setRate(rate);
		session.save(r);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GamesRate> findGameRateByUserGame(SessionFactory sessionFactory,
			Session session, int user_id, int game_id) {
		Criteria cr = session.createCriteria(GamesRate.class);
		
		Criterion user = Restrictions.eq("user_id", user_id);
		Criterion game = Restrictions.eq("game_id", game_id);
	
		LogicalExpression andExp = Restrictions.and(user, game);
		cr.add(andExp);
		return cr.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LocalRate> findLocalRateByUserGame(SessionFactory sessionFactory,
			Session session, int user_id, int local_id) {
		Criteria cr = session.createCriteria(LocalRate.class);
		
		Criterion user = Restrictions.eq("user_id", user_id);
		Criterion local = Restrictions.eq("local_id", local_id);
	
		LogicalExpression andExp = Restrictions.and(user, local);
		cr.add(andExp);
		return cr.list();
	}

	@Override
	public GamesRate idToGameRate(SessionFactory sessionFactory,
			Session session, int gameRate_id) {
		Object object = session.load(GamesRate.class, gameRate_id);
		GamesRate g = (GamesRate) object;
		return g;
	}

	@Override
	public LocalRate idToLocalRate(SessionFactory sessionFactory,
			Session session, int localRate_id) {
		Object object = session.load(LocalRate.class, localRate_id);
		LocalRate g = (LocalRate) object;
		return g;
	}

	@Override
	public String returnAverageRateByGame(SessionFactory sessionFactory,
			Session session, int game_id) {
		int s = 0;
		Query sumq = session.createSQLQuery(
				"select sum(rate) from gamesrates s where s.game_id = :game_id")
				.setParameter("game_id", game_id);
				List suml = sumq.list();
				if (suml.isEmpty() || suml.get(0) == null) s = 0;
				else{
				 Object sum = suml.get(0);
				s = Integer.parseInt(String.valueOf(sum));
				System.out.println(s);
				}
		
				Query countq = session.createSQLQuery(
						"select count(*) from gamesrates s where s.game_id = :game_id")
						.setParameter("game_id", game_id);
				List countl = countq.list();
				if(countl.isEmpty() || countl.get(0) == null || Integer.parseInt(String.valueOf(countl.get(0))) ==0) return "Brak ocen";
				Object count = countl.get(0);
				int c = Integer.parseInt(String.valueOf(count));
				System.out.println(c);
				
				
				return String.valueOf(Math.floor(s/c));
	}
	

	@Override
	public String returnAverageRateByLocal(SessionFactory sessionFactory,
			Session session, int local_id) {
		int s=0;
		Query sumq = session.createSQLQuery(
				"select sum(rate) from localsrates s where s.local_id = :local_id")
				.setParameter("local_id", local_id);
				List suml = sumq.list();
				if (suml.isEmpty() || suml.get(0) == null) s = 0;
				else{
				 Object sum = suml.get(0);
					s = Integer.parseInt(sum.toString());
				}
				
				Query countq = session.createSQLQuery(
						"select count(*) from localsrates s where s.local_id = :local_id")
						.setParameter("local_id", local_id);
				List countl = countq.list();
				if(countl.isEmpty() || countl.get(0) == null || Integer.parseInt(String.valueOf(countl.get(0))) ==0) return "Brak ocen";
				Object count = countl.get(0);
				int c = Integer.parseInt(count.toString());
				
				
				return String.valueOf(Math.floor(s/c));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GamesRate> findGameRateByUser(SessionFactory sessionFactory,
			Session session, int user_id) {
	Criteria cr = session.createCriteria(GamesRate.class);
		Criterion user = Restrictions.eq("user_id", user_id);
		cr.add(user);
		return cr.list();
	}

	

}
